package UI;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

import DBTasks.AddEvent;

@SuppressWarnings("serial")
public class EventsPanel extends JPanel implements ActionListener{

	private JButton eventButton, submitButton;
	private DefaultTableModel model;
	private JTable table;
	private JTableHeader headers;
	private JComboBox typeList;
	private String[] row = {"Adoption", "", "", ""};;
	private Object[][] data = {row};
	private String shelterName;;
	private MainFrame frame;
	private JButton returnButton;
	
	public EventsPanel(MainFrame mainFrame) {
		
		this.setSize(950, 600);
		this.frame = mainFrame;
		
		JLabel logo = new JLabel("Atlanta Pet Adoption Consortium");
		logo.setFont(new Font("Tahoma", Font.BOLD, 30));
		
		shelterName = (String) frame.getLogin().getShelterList().getSelectedItem();
		JLabel shelterLabel = new JLabel("Add New Events for " + shelterName);
		shelterLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
		
		eventButton = new JButton("Create New Event");
		eventButton.addActionListener(this);
		submitButton = new JButton("Submit Events");
		submitButton.addActionListener(this);
		
		returnButton = new JButton("Return");
		returnButton.addActionListener(this);
		
		String[] types = {"Adoption", "Fundraising"};
		typeList = new JComboBox(types);
		typeList.setSelectedIndex(0);
		typeList.addActionListener(this);
		
		String[] columnNames = {"Type", "Location", "Date", "Description"};
		model = new DefaultTableModel(data, columnNames);
		table = new JTable(model);
		table.setGridColor(Color.black);
		headers = table.getTableHeader();
		table.setBorder(BorderFactory.createLineBorder(Color.black));
		headers.setBorder(BorderFactory.createLineBorder(Color.black));
		table.setRowHeight(30);
		
		TableColumn typeColumn = table.getColumnModel().getColumn(0);
		typeColumn.setCellEditor(new DefaultCellEditor(typeList));
		DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
	    renderer.setToolTipText("Click for combo box");
	    typeColumn.setCellRenderer(renderer);
		
        GroupLayout gl = new GroupLayout(this);
		
		gl.setHorizontalGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
				.addComponent(logo)
				.addComponent(shelterLabel)
				.addComponent(headers)
				.addComponent(table)
				.addGroup(gl.createSequentialGroup()
						.addComponent(eventButton)
						.addGap(400)
						.addComponent(submitButton)
						.addComponent(returnButton)));
		
	    gl.setVerticalGroup(gl.createSequentialGroup()
	    		.addGap(15)
			    .addComponent(logo)
			    .addGap(40)
			    .addComponent(shelterLabel)
			    .addGap(20)
			    .addComponent(headers)
			    .addComponent(table)
			    .addGap(20)
			    .addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
						.addComponent(eventButton)
						.addComponent(submitButton)
						.addComponent(returnButton))
			    .addGap(40)
	    );
		
		gl.setAutoCreateContainerGaps(true);
		
		this.setLayout(gl);
		
	}
	
	public void paintComponent(Graphics g) {
        super.paintComponents(g);
        g.setColor(Color.blue);
        g.fillRect(0, 0, this.getWidth(), 70);
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource().equals(eventButton) && model.getRowCount() <= 15) {
			DefaultTableModel model = (DefaultTableModel) table.getModel();
			model.addRow(new Object[]{"Adoption", "", "", ""});
			frame.setPanel(frame.getEvents());
		}
		
		else if(e.getSource().equals(returnButton)) {
			frame.setEvents(new EventsPanel(frame));
			frame.setPanel(frame.getHomepage());
		}
		
		else if(e.getSource().equals(submitButton)) {
			int count = 0;
			for(int i = 0; i < data.length; i++) {
				if(AddEvent.addEvent(model.getValueAt(i, 2).toString(), model.getValueAt(i, 1).toString(), 
									 model.getValueAt(i, 3).toString(), model.getValueAt(i, 0).toString(), shelterName)) {
					count++;
				}
			}
			frame.setEmployeePanels();
			System.out.println(count + " events were accepted. The rest were discarded.");
			frame.setPanel(frame.getHomepage());
		}
		
	}

}
